﻿using System;
using System.Text;
using Abp.Dependency;
using Abp.Extensions;
using Microsoft.Extensions.Configuration;
using Microsoft.IdentityModel.Tokens;

namespace AbpProjectTemplate.Web.Authentication.JwtBearer
{
    public class JwtAuthOptions
    {
        public const string TokenQueryName = "auth_token";
        public const string TokenCookieName = "AbpProjectTemplate.AuthToken";

        public SymmetricSecurityKey SecurityKey { get; set; }

        public string Issuer { get; set; }

        public string Audience { get; set; }

        public SigningCredentials SigningCredentials { get; set; }

        public TimeSpan Expiration { get; set; }

        public bool SupportCookieAuthentication { get; set; }

        public bool SupportQueryAuthentication { get; set; }

        public JwtAuthOptions(IConfiguration configuration)
        {
            var section = configuration.GetSection("Authentication:JwtBearer");
            this.Issuer = section[nameof(this.Issuer)];
            this.Audience = section[nameof(this.Audience)];
            this.Expiration = TimeSpan.FromMinutes(section["ExpireInMinutes"].To<int>());
            this.SupportCookieAuthentication = section[nameof(this.SupportCookieAuthentication)]?.To<bool>() ?? true;
            this.SupportQueryAuthentication = section[nameof(this.SupportQueryAuthentication)]?.To<bool>() ?? true;

            this.SecurityKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(section[nameof(this.SecurityKey)]));
            this.SigningCredentials = new SigningCredentials(this.SecurityKey, SecurityAlgorithms.HmacSha256);
        }
    }
}


